//mock数据模拟
import Mock from 'mockjs'
import {getData} from "../index";
//import config from "vue/src/core/config";

let List=[];
export default{
  listUser: config=>{
    //console.log(config.body);
    const {name, page=1, limit=10} = JSON.parse(config.body);
    let indexpage=(page-1)*limit;
    if (List.length==0){
      for (let i=0;i<100;i++){
        List.push({
          id: crypto.randomUUID(),
          name: '张三',
          age:Mock.Random.int(10,30),
          sex:Mock.Random.int(0,1),
          birth: "2022-11-19",
          addr:'黑龙江省牡丹江市'
        });
      }
    }
    let arr=[];
    if (name != ""){
      List.forEach(function (v, index) {
        if (v.name==name){
          arr.push(v);
        }
      });
      if (arr.length==0){
        return {
          code: 200,
          count: arr.length,
          list: []
        };
      }
    }
    if (arr.length>0){
      return {
        code: 200,
        count: arr.length,
        list: arr.slice(indexpage,indexpage+limit)
      };
    }
    return {
      code: 200,
      count: List.length,
      list: List.slice(indexpage,indexpage+limit)
    };
  },
  delUser: data=>{
    const { id } = JSON.parse(data.body);
    //console.log(JSON.parse(config.body));
    List.forEach((v,i)=>{
      if (v.id==id){
        List.splice(i,1);
      }
    });
    return{
      code: 200,
      message: "删除成功"
    };
  },
  updateUser: config=>{
    const {name,age,sex,birth,addr,id} = JSON.parse(config.body);
    //console.log(JSON.parse(config.body));
    List.forEach((v, i) => {
      if (id==v.id){
        //List.slice(i);
        v.name=name;
        v.age=age;
        v.sex=sex;
        v.birth=birth;
        v.addr=addr;
      }
    });
    return{
      code: 200,
      message: "修改成功"
    };
  },
  addUser: config=>{
    const {name,age,sex,birth,addr} = JSON.parse(config.body);
    //console.log(JSON.parse(config.body),"config")
    List.unshift({id: crypto.randomUUID(),name: name,age: age, sex: sex, birth: birth, addr: addr});
    return{
      code: 200,
      message: "添加成功"
    };
  }
}
